/*
 * Exploder
 * 
 * Tero Pyylampi (218437), Jussi Tuurinkoski (211594)
 *
 * Background layer for the game
 */

#include "LayerBackground.h"
#include "../effects/GameEffects.h"
#include "../../Common.h"

const char *DEFAULT_BACKGROUND = "background.jpg";
const char *DEFAULT_UPPER_BACKGROUND = "background_upper.jpg";

// default constructor for the game scene
LayerBackground::LayerBackground( GameScene *scene ) : mEditorScene( 0 ), mGameScene( scene ) {
	mBackgroundSprites[ 0 ] = mBackgroundSprites[ 1 ] = mBackgroundSprites[ 2 ] = 0;
	initializeBackground( DEFAULT_BACKGROUND, DEFAULT_UPPER_BACKGROUND );
}

// default constructor for the editor mode
LayerBackground::LayerBackground( EditorScene *scene ) : mEditorScene( scene ), mGameScene( 0 ) {
	mBackgroundSprites[ 0 ] = mBackgroundSprites[ 1 ] = mBackgroundSprites[ 2 ] = 0;
	initializeBackground( DEFAULT_BACKGROUND, DEFAULT_UPPER_BACKGROUND );
}

// default destructor
LayerBackground::~LayerBackground() {
	for( unsigned int i = 0; i < 3; ++i ) {
		if( mBackgroundSprites[ i ] != 0 ) {
			removeChild( mBackgroundSprites[ i ], true );
			removeChild( mUpperBackgroundSprites[ i ], true );
		}
	}
}

// initialize this scene
void LayerBackground::initializeBackground( const char *backgroundFile, const char *upperBackgroundFile ) {
	mBackgroundSpriteFile = backgroundFile;

	// destroy old sprites
	for( unsigned int i = 0; i < 3; ++i ) {
		if( mBackgroundSprites[ i ] != 0 ) {
			removeChild( mBackgroundSprites[ i ], true );
			removeChild( mUpperBackgroundSprites[ i ], true );
			removeChild( mSmokeEffects[ i ], true );
			mSmokeEffects[ i ]->release();
		}
	}

	// create sprite
	for( unsigned int i = 0; i < 3; ++i ) {
		mBackgroundSprites[ i ] = cocos2d::CCSprite::spriteWithFile( mBackgroundSpriteFile );
		mBackgroundSprites[ i ]->setScaleX( RATEX );
		mBackgroundSprites[ i ]->setScaleY( RATEY );
		mBackgroundSprites[ i ]->setPosition( ccp( WIN_WIDTH * 0.5f - WIN_WIDTH + i * WIN_WIDTH, WIN_HEIGHT / 2.0f ) );
		addChild( mBackgroundSprites[ i ] );

		mUpperBackgroundSprites[ i ] = cocos2d::CCSprite::spriteWithFile( upperBackgroundFile );
		mUpperBackgroundSprites[ i ]->setScaleX( RATEX );
		mUpperBackgroundSprites[ i ]->setScaleY( RATEY );
		mUpperBackgroundSprites[ i ]->setPosition( ccp( WIN_WIDTH * 0.5f - WIN_WIDTH  + i * WIN_WIDTH, WIN_HEIGHT * 1.25f ) );
		addChild( mUpperBackgroundSprites[ i ] );

		mSmokeEffects[ i ] = GameEffects::createBackroundSmokeEffect( WIN_WIDTH * 0.9452f - WIN_WIDTH + i * WIN_WIDTH, WIN_HEIGHT * 0.524f, 5.0f );
		mSmokeEffects[ i ]->retain();
		addChild( mSmokeEffects[ i ], 1 );
	}
}

// get the current background file
const char *LayerBackground::getBackgroundFile() {
	return mBackgroundSpriteFile;
}

const char *LayerBackground::getUpperBackgroundFile() {
	return mUpperBackgroundSpriteFile;
}

// update -function
void LayerBackground::update( cocos2d::ccTime dt ) {
	// TODO
}

// get the game scene
GameScene *LayerBackground::getGameScene() {
	return mGameScene;
}
	
// get editor scene
EditorScene *LayerBackground::getEditorScene() {
	return mEditorScene;
}

